<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>部门</title>
<script type="text/javascript">
var ctx = "${pageContext.request.contextPath}";

//初始化checkbox
jQuery("document").ready(function(){
	
	$.fn.zTree.init($("#treeDemo"), setting);
	
	jQuery("#all").click(function(){
			var flag = document.getElementById("all").checked;
	   	 	jQuery("[name=selId]:checkbox").each(function() {
	        		jQuery(this).attr("checked", flag);
	   		});
   });
	
	 $("#Pagination").paginate({
			count 		: '${count}',	//页数
			start 		: '${currentPageNo}',
			display     : 10,
			border					: true,
			border_color			: '#BEF8B8',
			text_color  			: '#68BA64',
			background_color    	: '#CAE8EA',	
			border_hover_color		: '#68BA64',
			text_hover_color  		: 'black',
			background_hover_color	: '#CAE8EA', 
			rotate      : false,
			images		: false,
			mouse		: 'press',
			onChange     			: function(page){
				                       $("#currentPageNo").val(page);
										var url = ctx+'/ufmDept/findAll';
									    document.form1.action=url;
										document.form1.submit();
									  }
		});
	 
	 
	 if($.inArray("部门维护_删除", allRights)>=0){
		   $("#delete").show();
		   $("a[name='delete1']").show();
	   }
	   if($.inArray("部门维护_新增", allRights)>=0){
		   $("#add").show();
	   }
	   if($.inArray("部门维护_修改", allRights)>=0){
		   $("a[name='edit']").show();
	   }
	   if($.inArray("部门维护_查询", allRights)>=0){
		   $("#find").show();
	   }
});






function addNew(){
	var deptId = $('#deptId').val();	
	var formObj = document.getElementById("form1");
    if(deptId==0){    	
    	formObj.action=ctx+'/ufmDept/addDept';
    	formObj.submit();
    }else{
    	var url = ctx+'/ufmDept/isDept';
    	url+= '?deptId='+deptId;	
    	$.ajax({
    	    url:url,
    	    type: "POST",	  
    	    cache : false,
    	    async:false,
    		error : function(XMLHttpRequest, textStatus, errorThrown) {
    			//	alert(XMLHttpRequest.readyState);
    			},
    			success: function(data) {    				
    			if(data == "isDept"){	
    	    		alert("部门下面不能创建部门");    			
    			}else{					
    				formObj.action=ctx+'/ufmDept/addDept';
    		    	formObj.submit();
    			} 		 
    	    }
    	});
   }
}

function search(){	
	var formObj = document.getElementById("form1");

	$("#currentPageNo").val(1);
	formObj.action=ctx+'/ufmDept/findAll?search=1';
	formObj.submit();
}

function lookUser(deptId){
	var formObj = document.getElementById("form1");
	$('#deptId').val(deptId);
	formObj.action=ctx+'/ufmDept/lookDept';
	formObj.submit();
}

function toUpdatePage(deptId){
	var formObj = document.getElementById("form1");
	$('#deptId').val(deptId);
	formObj.action=ctx+'/ufmDept/toUpdatePage';
	formObj.submit();
}

function deleteAll(){
	var formObj = document.getElementById("form1");
	var chk_value =[];
	$('input[name="selId"]:checked').each(function(){    
			chk_value.push(jQuery(this).val());
	});
	var selIds = chk_value.join();
	if(selIds==''){
		alert("请选择要删除的部门 !");
		return;	
	}
	if(window.confirm("确认删除部门?")){	
		
		var url = ctx+'/ufmDept/deleteAll';
		url+= '?selIds='+selIds;
		
		$.ajax({
		    url:url,
		    type: "GET",
		   // dataType:"html",
		    cache : false,
		    async:false,
			error : function(XMLHttpRequest, textStatus, errorThrown) {
   			//	alert(XMLHttpRequest.readyState);
   			},
   			success: function(data) {	
   				var strs = data.split(",");
   				if(strs[0] == "isChild"){	
		    		alert("不能删除编号为"+strs[1]+"的数据，因为数据中有子节点!");
				}else if(strs[0] == "isUser"){
					alert("不能删除编号为"+strs[1]+"的数据，请先删除部门下的人员!");
				}else if(strs[0] == "isModel"){
					alert("不能删除编号为"+strs[1]+"的数据，因为部门已在模板中使用!");
				}else{					
					formObj.action=ctx+'/ufmDept/findAll';
					formObj.submit();
				} 		 
		    }
		});
		
	}
} 

function deleteById(value){
	var formObj = document.getElementById("form1");
	
	 if(confirm("是否确认要删除?")){
		
			var url = ctx+'/ufmDept/deleteAll';
			   url += '?selIds='+value;	    
			$.ajax({
		    url:url,
		    type: "GET",
		   // dataType:"html",
		    cache : false,
		    async:false,
			error : function(XMLHttpRequest, textStatus, errorThrown) {
   			//	alert(XMLHttpRequest.readyState);
   			},
   			success: function(data) {
   				var strs = data.split(",");
   				
   				if(strs[0] == "isChild"){	
		    		alert("不能删除该条数据，因为数据中有子节点!");
				}else if(strs[0] == "isUser"){
					alert("不能删除该条数据，请先删除部门下的人员!");
				}else if(strs[0] == "isModel"){
					alert("不能删除该条数据，因为部门已在模板中使用!");
				}else{		
					formObj.action=ctx+'/ufmDept/findAll';
					formObj.submit();
				} 		 
		    }
		});
	 }
  }








function lookDept(deptId){
	var formObj = document.getElementById("form1");
	$('#deptId').val(deptId);
	formObj.action=ctx+'/ufmDept/lookDept';
	formObj.submit();
}

  
   // zTree
   var setting = {
			async: {
				enable: true,
				url:"${pageContext.request.contextPath}/ufmDept/getTree",
				autoParam:["M1", "level"]
			},
		   data: {		
			   simpleData: {
			          enable: true
		           }
	                 },
	     callback: {
		       onClick: zTreeOnClick
	           }

};
   // zTree 
   var zNodes = [
             <c:forEach items="${deptAll}" var="all" varStatus="i">           
                     {id:"${all.m1}", pId:"${all.m2}", name: "${all.m3}"},        
            </c:forEach>        

   ];
  
   
   function zTreeOnClick(event, treeId, treeNode) {
	
	     var d = treeNode.id;	  
	   $('#deptId').val(d);
	   var url1 = ctx+'/ufmDept/ajaxGetTree';
	    url1 += '?parentId='+d;
		 jQuery.ajax({
			    url:url1,
			    type: "POST",
			    async:false,
			    success: function(data) {
			    	$("#resultList").empty();
       				$("#resultList").html(data);
			    }
			});
	   
	   
	   
	};




</script>
</head>

<body>
  <div id="menu" style="overflow: scroll;height: 64%;width:20%">
	<ul id="treeDemo" class="ztree"></ul>
  </div>
  <div id="content"   style="height:500px">			
	<div>
	<form id="form1" name="form1" action="" method="post">
	          部门名称:<input type="text" id="name" name="name" value="${name }" /> &nbsp;&nbsp;
	      <input id="find" type="button" style="display: none" value="查询" onclick="search()"/>&nbsp;&nbsp;
	      <input id="add" type="button" style="display: none" value="新增" onclick="addNew()"/>&nbsp;&nbsp;	
	      <input id="delete" type="button" style="display: none" value="删除" onclick="deleteAll()"/>&nbsp;&nbsp;
	      <input type="hidden"  id="deptId" name="deptId" value="${deptId}"/>
	       <input type="hidden" id="currentPageNo" name="currentPageNo" value="${currentPageNo}"/>
	      <input type="hidden" id="perPageNumber" name="perPageNumber" value="${perPageNumber}"/>
	       </form>
     </div>
<div id="resultList">
<table id="mytable" cellspacing="0" summary="The technical specifications of the Apple PowerMac G5 series">  
  <tr>
  	<th><input type="checkbox" id="all" name="all"/></th> 
    <th>序号</th> 
    <th>编号</th> 
    <th>名称</th>
    <th>简码</th>   
    <th>顺序号</th>        
    <th>级别</th>
    <th>联系人</th>
    <th>联系电话</th>
    <th>创建人</th>
    <th>创建时间</th>
    <th>操作</th> 
  </tr> 
  <c:forEach items="${dept}" var="d" varStatus="i">
  	<tr> 
  		<td align="center"><input type="checkbox" id="selId" name="selId" value="${d.m1}"/></td> 
	   	<td>${i.index+1}</td>
	   	<td>${d.m1}</td>
	    <td>${d.m3}</td> 
	    <td>${d.p11}</td> 
	    <td>${d.p12}</td>	  
	    <td>${d.z4}</td> 
	    <td>${d.m9}</td>	    
	    <td>${d.m10}</td> 
	    <td>${d.p13}</td> 
	    <td><fmt:formatDate value="${d.p14}" type="date" pattern="yyyy-MM-dd HH:mm:ss"/>  </td>
	    <td><a name="delete1" style="display: none" href="javascript:void(0)" onclick = "deleteById('${d.m1}')">删除</a>
	    	<a name="edit" style="display: none" href="javascript:void(0)" onclick="toUpdatePage('${d.m1}')">修改</a></td>
  	</tr> 
  </c:forEach>
  
</table>
<div id="Pagination" ></div>  
</div>  
</div>
</body>
</html>
